#ifndef	__CACHE_H__
#define   __CACHE_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//some need define variable from other file(flash.h)
#define SECT_NUM_PER_PAGE  4
#define PAGE_NUM_PER_BLK  64
#define SECT_NUM_PER_BLK  (SECT_NUM_PER_PAGE * PAGE_NUM_PER_BLK)
#define SECT_SIZE_B 512
//shzb:cache delay
#define CACHE_READ_DELAY  0.0005
#define CACHE_WRITE_DELAY 0.0005
//ZhouJie:cache status
#define CACHE_INVALID 0
#define CACHE_REAL 1
#define CACHE_GHOST 2//用于缓存管理算法
#define CACHE_SECOND 3
#define	CACHE_VALID 4
//zhoujie:cache statistics
int CACHE_CLRU_NUM_ENTRIES;
int CACHE_DLRU_NUM_ENTRIES;
int CACHE_NUM_ENTRIES;
//all page number 
int opagemap_num;
//select ftl algortihm(1:pure page FTL,2:block FTL,3:DFTL,4:fast)
extern int ftl_type;
extern int cache_type;
//cache  (read/write count)variable 
int cache_read_num;
int cache_write_num;
//this variable exsit in pagemap.c
int TOTAL_MAP_ENTRIES; 
//NOTE!!!:change opm_entry in dftl.h 
struct opm_entry {
  int free  : 1;
  int ppn   : 31;
  int  cache_status;
  int  cache_age;
  int  map_status;
  int  map_age;
  int  update;
  int  cache_update;//缓冲区页的更新标志
};
struct opm_entry *opagemap;

//这里存储的是数据
//cache algorithm(buffer size)-zhoujie
//LRU-buffer
#define CACHE_MAX_ENTRIES 1024
//CASA-buffer DLRU+CLRU=LRU(max)
#define CACHE_CLRU_MAX_ENTRIES 1024
#define CACHE_DLRU_MAX_ENTRIES 1024
/***********************************************************************
  Cache buffer array 
 ***********************************************************************/
int cache_lru_arr[CACHE_MAX_ENTRIES];
int cache_dlru_arr[CACHE_MAX_ENTRIES];
int cache_clru_arr[CACHE_MAX_ENTRIES];
int cache_clru_cur_size;
int cache_dlru_cur_size;
extern int cache_max_size;

//function declaration 
void init_arr();
int opm_init(int blk_num);// 这个函数需要注意修改的地方
int ComputeArrSize(int *arr,int MaxSize);
int find_free_pos( int *arr, int size);
int find_cache_min(int arr[],int arrMaxSize);
int find_cache_max(int arr[],int arrMaxSize);
int search_table(int *arr, int size, int val) ;
double calculate_delay_cache();
void reset_cache_stat();
double cacheManage(unsigned int secno, int scount, int operation);
#endif
